package webapp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import mda.type.IRegisterTemplates;
import flca.mda.codegen.data.ITemplate;
import flca.mda.codegen.data.SubsValue;
import flca.mda.codegen.data.SubsValueType;
import flca.mda.codegen.data.TemplatesBranch;
import flca.mda.codegen.helpers.ShellUtils;
import flca.mda.common.api.helpers.RegisterTemplatesHelper;

public class RegisterTemplates implements IRegisterTemplates, WebappConstants {
	@Override
	public String getName() {
		return "webapp";
	}

	@Override
	public String getDescription() {
		return "enterprise webapp";
	}

	@Override
	public String getVersion() {
		return "2.010";
	}

	@Override
	public List<TemplatesBranch> getTemplateBranches() {
		List<TemplatesBranch> result = new ArrayList<TemplatesBranch>();

		result.add(makeBranch(new JavaConstants(), "backend.jpa", "java backend based on JPA persistence"));
		result.add(makeBranch(new FlexConstants(), "frontend.flex", "Frontend based on Flexbuilder 3"));
		result.add(makeBranch(new ZkConstants(), "frontend.zk", "Frontend based on ZK"));
		result.add(makeBranch(new DojoConstants(), "frontend.dojo", "Frontend based on Dojo1.7"));

		return result;
	}

	private TemplatesBranch makeBranch(TemplatesConstBase constants, String aName, String aDescr) {
		TemplatesBranch result = new TemplatesBranch();
		result.setName(aName);
		result.setDescription(aDescr);

		for (ITemplate template : constants.getAllTemplates()) {
			result.addTemplate(template);
		}

		return result;
	}

	private final static SubsValueType TEXT = SubsValueType.TEXTINPUT;
	private final static SubsValueType FOLDER = SubsValueType.FOLDER;
	private final static String[] ALL_WEBAPP_CARTRIDGES = new String[] {
		JavaConstants.CARTRIDGE_NAME, ZkConstants.CARTRIDGE_NAME, FlexConstants.CARTRIDGE_NAME, DojoConstants.CARTRIDGE_NAME
	};
	
	@Override
	public Collection<SubsValue> getSubstituteValues() {

		Collection<SubsValue> result = RegisterTemplatesHelper.getCommomSubsValues(ALL_WEBAPP_CARTRIDGES);
		
		result.add(makeSubsValue(BACKEND, FOLDER, "backend", "target directory for backend code", 2, null, 
				new String[] {JavaConstants.CARTRIDGE_NAME, ZkConstants.CARTRIDGE_NAME} ));
		result.add(makeSubsValue(SCR_GEN, TEXT, "scr-gen", "sub directory for generated sources", 3, getTargetSourceDir(false),  
				new String[] {JavaConstants.CARTRIDGE_NAME, ZkConstants.CARTRIDGE_NAME, FlexConstants.CARTRIDGE_NAME} ));
		result.add(makeSubsValue(SCR_GEN_TEST, TEXT, "scr-gen-test", "sub directory for generated test sources", 4, getTargetSourceDir(true), 
				new String[] {JavaConstants.CARTRIDGE_NAME, ZkConstants.CARTRIDGE_NAME, FlexConstants.CARTRIDGE_NAME} ));
		result.add(makeSubsValue(FRONTEND_ZK, FOLDER, "frontend-zk", "target directory for Zk code", 4, null, 
				new String[] {ZkConstants.CARTRIDGE_NAME} ));
		result.add(makeSubsValue(FRONTEND_FLEX, FOLDER, "frontend-flex", "target directory for Flex code", 4, null, 
				new String[] {FlexConstants.CARTRIDGE_NAME} ));
		result.add(makeSubsValue(FRONTEND_DOJO, FOLDER, "frontend-dojo", "target directory for Dojo code", 4, null, 
				new String[] {DojoConstants.CARTRIDGE_NAME} ));
		
		result.add(makeComboSubsValue(SERVER, "server", 
				new String[] {SERVER_JETTY, SERVER_TOMCAT}, "target server", 5, SERVER_JETTY,
				new String[] {JavaConstants.CARTRIDGE_NAME} ));

//		result.add(makeSubsValue(FRONTEND_WEB, text, "frontend-web", "sub directory for web code", 4, "web", ));

		return result;
	}

	private static final String S = ShellUtils.getPathDelim();
	
	/**
	 * return the default target source subdir : "/src/main/src-gen-java" resp "/src/test/src-gen-java" 
	 * @param aTestDir
	 * @return
	 */
	private String getTargetSourceDir(boolean aTestDir) {
		if (aTestDir) {
			return  "src" + S + "test" + S + "src-mda-gen";
		} else {
			return  "src" + S + "main" + S + "src-mda-gen";
		}
	}
	
	// --- SubsValue for webapp

	private SubsValue makeSubsValue(String name, SubsValueType type, String label, String help, 
			int rank, String aDefaultValue, String aForCartridges[]) {
		return RegisterTemplatesHelper.makeSubsValue(name, type, label, help, rank, aDefaultValue, aForCartridges);
	}

	private SubsValue makeComboSubsValue(String name, String label, String[] items, String help, 
			int rank, String aDefaultValue, String aForCartridges[]) {
		return RegisterTemplatesHelper.makeComboSubsValue(name, label, items, help, rank, aDefaultValue, aForCartridges);
	}

}
